<?php

/**
 * PagesController replaces standart cakephp PagesController. It looks for page in database table 'pages' and then in /views/pages directory.
 *
 * @author jblew
 */
class PagesController extends AppController {
    var $name = 'Pages';

    var $helpers = array('Html', 'Form', 'Session');
    var $components = array('Session');

    private $disallowedPages = array("display", "admin_index", "admin_view", "admin_delete", "admin_edit", "admin_create");

    public function __construct() {
        parent::__construct();
    }

    function display() {
        $path = func_get_args();

        $count = count($path);
        if (!$count) {
            $this->redirect('/');
        }
        $page = $subpage = $title_for_layout = null;

        if (!empty($path[0])) {
            $page = $path[0];
        }
        if (!empty($path[1])) {
            $subpage = $path[1];
        }
        if (!empty($path[$count - 1])) {
            $title_for_layout = Inflector::humanize($path[$count - 1]);
        }

        $result = $this->Page->findByUrlname($path);

        if ($result != false) {
            $page_title = $result['Page']['title'];
            $page_content = $result['Page']['content'];
            $this->set('title_for_layout', $page_title);
            $this->set(compact('page', 'subpage', 'page_title', 'page_content'));
            $this->render('display');
        } elseif (!in_array($page, $this->disallowedPages)) {
            $this->set(compact('page', 'subpage', 'title_for_layout'));
            $this->render(implode('/', $path));
        } else {
            $this->trigger404();
        }
    }

    public function admin_index() {
        $this->title = "Statyczne strony";

        $result = $this->Page->find('all');
        $this->set('pages', $result);
    }

    public function admin_view($id) {
        $result = $this->Page->find('first', array(
                    'conditions' => array(
                        'id' => $id
                    )
                ));

        $this->title = "Strona: ".$result['Page']['title'];

        $this->set('page', $result['Page']);
    }

    function admin_edit($id = null) {
        $this->helpers[] = "CKEditor";
        $this->title = "Edycja strony";

	$this->Page->id = $id;
	if (empty($this->data)) {
		$this->data = $this->Page->read();
	} else {
		if ($this->Page->save($this->data)) {
			$this->Session->setFlash('Zapisano stronę.');
			$this->redirect(array('action' => 'edit', $id));
		}
	}
}


    public function admin_delete($id) {
        $this->title = "Usuwanie strony";

        if ($this->Page->delete($id)) {
            $this->Session->setFlash('Usunięto.');
            $this->redirect(array('action' => 'index'));
        }
    }

    public function admin_create() {
        $this->title = "Nowa strona";

        $this->helpers[] = "CKEditor";
        if (!empty($this->data)) {
            if ($this->Page->save($this->data)) {
                $this->Session->setFlash('Zapisano stronę.');
                $this->redirect(array('action' => 'index'));
            }
        }
    }

}
?>
